<?php
namespace app\user\model;

use think\Model;

class Set extends Model
{
    /*小公排
     *
     *uid 推荐人id
     * 调用方法          $set=model('Set');
                         $set->xiaogongpai($reus['id'],$uid);
     */
    public function xiaogongpai($reid,$uid){
        $reuser=getuser($reid);
        $rppath='';
        if($reuser['recount']>0){
            $erbu=db('users')->where('id',$reuser['rightid'])->find();
            if(empty($erbu)){
                $us=$reuser;
                $rppath=",".$uid.",";
                db('users')->where('id',$uid)->setField('rightppath',$rppath);
            }else{
                $rpath=trim($erbu['rightppath'],",");
                $rpatharr=explode(",",$rpath);
                $path=array_reverse($rpatharr);
                $us=getuser($path[0]);
            }
        }else{
                $rpath=trim($reuser['rightppath'],",");
                $rpatharr=explode(",",$rpath);
                $path=array_reverse($rpatharr);
                $us=getuser($path[0]);


        }
/*
        $ppath=$reuser['ppath'].$reuser['id'];
        $us=db('users')
            ->where("ppath like '%".$ppath."%' or id=".$reuser['id'])
            ->where('leftid=0  or rightid=0')
            ->order('plevel asc,pid asc')
            ->find();*/
        if($us){
            if($us['leftid']==0){
                $treeplace=1;
                db('users')->where('id',$us['id'])->setField('leftid',$uid);
                $rightppath="".$us['rightppath'].$uid.",";
                db('users')
                    ->where('id','in',$rightppath)
                    ->update(array('rightppath'=>$rightppath));
            }elseif($us['rightid']==0){
                $treeplace=2;
                db('users')->where('id',$us['id'])->setField('rightid',$uid);
            }
            $member_update=array();
            $member_update['treeplace']=$treeplace;
            $member_update['fatherid']=$us['id'];
          //  $member_update['rightppath']=$rppath;
            $member_update['fathername']=$us['nickname'];
            $member_update['plevel']=$us['plevel']+1;
            $pid=db('users')->where('activation',1)->order('pid desc')->find();
            $member_update['pid']=$pid['pid']+1;
            $member_update['ppath']="".$us['ppath'].$us['id'].",";
            $member_update['jihuo_time']=time();
            db('users')->where('id',$uid)->update($member_update);
        }
    }
/*    function bonus2($id){
        $user=getuser($id);
        $pusers=db('users')
            ->where('id','in',$user['ppath'])
            ->where('plevel','between',[0,$user['plevel']])
            ->select();
        if(!empty($pusers)){
            foreach ($pusers as $key=>$v){
                $level=getulevel($v['level']);
                $cha=$user['plevel']-$v['plevel'];
                if($cha==1){
                    if($user['up_userid']==$v['id']){
                        $money=$level['b1'];
                        $source=11;
                    }else{
                        $money=$level['b2'];
                        $source=12;
                    }
                }elseif($cha==2){
                    if($user['up_userid']==$v['id']){
                        $money=$level['b1'];
                        $source=11;
                    }else{
                        $money=$level['b2'];
                        $source=12;
                    }
                }else{
                    $money=0;
                    $source=0;
                }
                if($money>0){
                    $beizhu=$user['nickname'];
                    jifen(2,$v['id'],$money,$source,1,$beizhu);
                    achievement(0,0,0,0,$money,$money,$money,0,0);
                }
                $plevel=$v['plevel']+2;
             //   Model('set')::bonus3($v['id'],$plevel,$v['ppath']);
            }
        }
    }

    function bonus3($id,$plevel,$ppath){
        $addvpath=$ppath.$id;
        $num=db('users')
            ->where('ppath','like','%'.$addvpath.'%')
            ->where('plevel',$plevel)
            ->where('activation',1)
            ->count();
        if($num>=4){
            $user=getuser($id);
            db('users')->where('id',$id)->setInc('money_jifen',$user['money_qian']);
            db('users')->where('id',$id)->setField('money_qian',0);
            db('users')->where('id',$id)->setField('out',1);
        }
    }
    //插入会员
    function insert_user($id){
        $user=getuser($id);
        $info['uid']=$id;
        $info['nickname']=getnick();
        $info['mobile']=$user['mobile'];
        $info['up_userid']=$user['up_userid'];
        $info['rpath']=$user['rpath'];
        $info['relevel']=$user['relevel'];
        $info['bank_name']=$user['bank_name'];
        $info['bank_card']=$user['bank_card'];
        $info['bank_dizhi']=$user['bank_dizhi'];
        $info['zhifubao']=$user['zhifubao'];
        $info['wechat']=$user['wechat'];
        $info['password']=$user['password'];
        $info['twopassword']=$user['twopassword'];
        $info['dizhi']= $user['dizhi'];
        $info['province']=$user['province'];
        $info['city']=$user['city'];
        $info['district']=$user['district'];
        $info['shenfenzheng']=$user['shenfenzheng'];
        $info['activation']=1;
        $info['reg_time'] = time();
        $info['jihuo_time'] = time();
        $uid = db('users')->insertGetId($info);
        $up=db('users')
            ->where("(uid=".$user['up_userid']." or id=".$user['up_userid'].") and out=0")
            ->find();
        $set=model('Set');
        $set->xiaogongpai($up['id'],$uid);
        bonus2($uid);
    }*/


}